#!/bin/bash
#author:wangjun
#purpose:monitoring ftp service
#date :2016/5/12
###########
#jiankong
##########
monitor(){
a_stop=0
a_start=0
 >MFS.log
while true
do
	d_date=$(date +%s_%H:%M:%S)
	if nc -z 192.168.0.189 21  &>/dev/null
	then
		a_stop=0
		echo "$d_date ftp server is start"
		((a_start==0))&&echo "$d_date ftp server is start" >>MFS.log
		((a_start++))
	else	
		a_start=0
		echo "$d_date warning!!ftp server is down"
		((a_stop==0))&&echo "$d_date warning!!ftp server is down" >>MFS.log
		((a_stop++))   ##a=1 also ok
	fi
	sleep 1
done
}
#######
#output
#######
output(){
a=1
i=0
t_time=($(cat MFS.log |tr "_" " "|cut -d" " -f1))
cat MFS.log |tr "_" " "|cut -d" " -f2-10 >mfs.txt
f_number=$(cat MFS.log |head -n +1|wc -w)
while read all
do
	echo "$all"
	jiange=$(echo "${t_time[$a]}-${t_time[$i]}"|bc)
	if ((jiange>=0))
	then
		echo "${jiange}s后"
		
	else
		echo "监控结束"
	fi
 	((i++))
	((a=i+1))
done <mfs.txt
}
#####
#menu
#####
echo -e "\t\t\t1.监控ftp，Ctrl+c结束"
echo -e "\t\t\t2.查看日志"
echo -e "\t\t\t3.退出"

read -p "what's your choice:" c_choice
case $c_choice in
1)
monitor
;;
2)
output
;;
*)
exit
;;
esac
